{extend name="common/base"}
{block name="content"}
<style>
    #add-user  , #xuqi{
        color: #000!important;
    }
    #add-user .am-u-md-12,#add-user .am-form-field {
        margin-bottom: 10px;
        color: #000!important;
    }
    #xuqi .am-u-md-12,#xuqi .am-form-field {
        margin-bottom: 10px;
        color: #000!important;
    }
</style>
<div class="row-content am-cf">
    <div class="row">
        <div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
            <div class="widget am-cf">
                <div class="widget-head am-cf">
                    <div class="widget-title  am-cf">机器人账号列表</div>


                </div>
                <div class="widget-body  am-fr">

                    <div class="am-u-sm-12 am-u-md-6 am-u-lg-6">
                        <div class="am-form-group">
                            <div class="am-btn-toolbar">
                                <button class="am-btn-success am-btn" data-am-modal="{target: '#add-user', closeViaDimmer: 0, width: 400}">添加机器人账号</button>
                            </div>
                        </div>
                    </div>

                    <div class="am-u-sm-12">
                        <table width="100%" class="am-table am-table-compact am-table-striped tpl-table-black ">
                            <thead>
                            <tr>
                                <th>账号</th>
                                <th>过期时间</th>
                                <th>类型</th>
                                <th>群名</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody id="ajax-wrapper">

                            </tbody>
                        </table>
                    </div>
                    <div class="am-u-lg-12 am-cf">
                        <div class="am-fr ajax-page">
                            {$page}
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
{/block}
{block name="footer-js"}
<div class="am-modal am-modal-no-btn" tabindex="-2" id="add-user">
    <div class="am-modal-dialog">
        <div class="am-modal-hd"> 添加账号
            <a href="javascript: void(0)" class="am-close am-close-spin" data-am-modal-close>&times;</a>
        </div>
        <form action="" class="my-form">
            <div class="am-modal-bd">
                <div class="am-u-md-12">
                    <input type="text" name="account" class="am-form-field" placeholder="账号">
                </div>
                <div class="am-u-md-12">
                    <input type="number" name="expire_time" class="am-form-field" placeholder="有效期（天）">
                </div>
                <div class="am-u-md-12">
                    <input type="text" name="type" class="am-form-field" placeholder="类型（1：qq，2：微信）">
                </div>
                <div class="am-u-md-12">
                    <input type="text" name="group" class="am-form-field" placeholder="群名">
                </div>
            </div>
        </form>
        <div class="am-u-md-12">
            <button class="am-btn-success am-btn" id="submit" type="button">提交</button>
        </div>
    </div>
</div>


<div class="am-modal am-modal-no-btn" tabindex="-2" id="xuqi">
    <div class="am-modal-dialog">
        <div class="am-modal-hd"> 续期
            <a href="javascript: void(0)" class="am-close am-close-spin" data-am-modal-close>&times;</a>
        </div>
        <form action="" class="my-form">
            <div class="am-modal-bd">
                <div class="am-u-md-12">
                    <input type="number" name="expire_time" class="am-form-field" placeholder="单位：天">
                </div>
            </div>
        </form>
        <div class="am-u-md-12">
            <button class="am-btn-success am-btn submit"  type="button">提交</button>
        </div>
    </div>
</div>

<div class="am-modal am-modal-no-btn" tabindex="-2" id="bianji">
    <div class="am-modal-dialog">
        <div class="am-modal-hd"> 编辑报奖选项
            <a href="javascript: void(0)" class="am-close am-close-spin" data-am-modal-close>&times;</a>
        </div>
        <form action="" class="my-form">
            <div class="am-modal-bd">
                <div class="am-u-md-12">
                    pk10：<input type="checkbox" name="data_type[]" value="50" class="am-switch am-round am-switch-default" />
                    时时彩：<input type="checkbox" name="data_type[]" value="1" class="am-switch am-round am-switch-default" />
                    飞艇：<input type="checkbox" name="data_type[]" value="51" class="am-switch am-round am-switch-default" />
                </div>
            </div>
        </form>
        <div class="am-u-md-12">
            <button class="am-btn-success am-btn submit"  type="button">提交</button>
        </div>
    </div>
</div>


<div class="am-modal am-modal-alert" tabindex="-1" id="add-user">
    <div class="am-modal-dialog">
        <div class="am-modal-hd">删除账号</div>
        <div class="am-modal-bd">
            你确定要删除吗？
        </div>
        <div class="am-modal-footer">
            <div class="am-modal-footer">
                <span class="am-modal-btn noconfirm">取消</span>
                <span class="am-modal-btn confirm" >确定</span>
            </div>
        </div>
    </div>
</div>

<div class="am-modal am-modal-alert" tabindex="-1" id="sanchu">
    <div class="am-modal-dialog">
        <div class="am-modal-hd">删除账号</div>
        <div class="am-modal-bd">
            你确定要删除吗？
        </div>
        <div class="am-modal-footer">
            <div class="am-modal-footer">
                <span class="am-modal-btn noconfirm">取消</span>
                <span class="am-modal-btn confirm" >确定</span>
            </div>
        </div>
    </div>
</div>

</div>
<script src="__ADMIN__/template.js"></script>
<script id="ajax-content" type="text/html">
    <%for(var i = 0 ; i<data.length; i++) {%>
    <tr>
        <td><%=data[i].account%></td>
        <td><%=data[i].expire_time%></td>
        <td><%=data[i].type%></td>
        <td><%=data[i].group%></td>
        <td>
            <button type="button" class="am-btn am-btn-warning bianji"  data-id="<%=data[i].id%>">报奖选项</button>
            <button type="button" class="am-btn am-btn-success xuqi"  data-id="<%=data[i].id%>">续期</button>
            <button type="button" class="am-btn am-btn-danger sanchu"  data-id="<%=data[i].id%>">删除</button>
        </td>
    </tr>
    <%}%>
</script>
<script>
    var config = {
        success : function (res) {
            // console.log(res)
            var html = template($('#ajax-content').html() , {data:res.ad_list.data})

            $('#ajax-wrapper').html(html)
            $('.ajax-page').html(res.page)
        }
    };
    $.ajax(config);
    $('.ajax-page').on('click' , 'a' , function (e) {
        e.preventDefault();
        var url = $(this).attr('href');
        config.url = url;
        $.ajax(config);
    })

    $('#submit').click(function () {
        var fd = new FormData($('.my-form')[0]);
        $.ajax({
            type : 'post' ,
            url : '{:url("addUser")}',
            data :fd,
            // 告诉jQuery不要去处理发送的数据
            processData : false,
            // 告诉jQuery不要去设置Content-Type请求头
            contentType : false,
            success : function (res) {
                $('#add-user').modal('close')
                $.ajax(config);
            }
        });
    });

    $('#ajax-wrapper').on('click' , '.sanchu' , function () {

        $('#sanchu').data('id' , $(this).data('id'));
        $('#sanchu').modal();
        $('#sanchu .confirm').click(function () {

            $.ajax({
                type : 'post' ,
                url : '{:url("delete")}',
                data :'id='+$('#sanchu').data('id'),
                success : function (res) {
                    $('#sanchu').modal('close')
                    $.ajax(config);
                }
            });
        })
        $('#sanchu .noconfirm').click(function () {
            $('#sanchu').modal('close')
        });
    })

    $('#ajax-wrapper').on('click' , '.xuqi' , function () {

        $('#xuqi').data('id' , $(this).data('id'));
        $('#xuqi').modal();
        $('#xuqi .submit').click(function () {

            var fd = new FormData($('.my-form')[1]);
            fd.append('id' , $('#xuqi').data('id'));
            $.ajax({
                type : 'post' ,
                url : '{:url("xuqi")}',
                data :fd,
                // 告诉jQuery不要去处理发送的数据
                processData : false,
                // 告诉jQuery不要去设置Content-Type请求头
                contentType : false,
                success : function (res) {
                    $('#xuqi').modal('close')
                    $.ajax(config);
                }
            });
        })

    })

    $('#ajax-wrapper').on('click' , '.bianji' , function () {
        $('#bianji').data('id' , $(this).data('id'));

        $('[name="data_type[]"]').parent().parent().removeClass('am-active');
        $('[name="data_type[]"]').prop('checked' , false);
        $.ajax({
            url : '{:url("getbianji")}',
            data: 'id='+  $('#bianji').data('id'),
            success : function (res) {
                $('[name="data_type[]"]').each(function () {
                    var arr = res.split(',');
                    console.log(arr)
                    for (var i = 0; i<arr.length; i++) {
                        if ($(this).val() == arr[i]) {
                            $(this).parent().parent().addClass('am-active');
                            $(this).prop('checked' , true);
                        }
                    }
                })
            }
        });


        $('#bianji').modal();
        $('#bianji .submit').click(function () {

            var fd = new FormData($('.my-form')[2]);
            fd.append('id' , $('#bianji').data('id'));
            $.ajax({
                type : 'post' ,
                url : '{:url("bianji")}',
                data :fd,
                // 告诉jQuery不要去处理发送的数据
                processData : false,
                // 告诉jQuery不要去设置Content-Type请求头
                contentType : false,
                success : function (res) {
                    $('#bianji').modal('close')
                    $.ajax(config);
                }
            });
        })

    })
</script>
{/block}